const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    executablePath: 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe',   //指定chromium浏览器位置;
    headless: false,  //默认为true 表示不打开浏览器展示;
    timeout: 100000     //超时操作 默认为30s 1000ms=1s;
  });
  const page = await browser.newPage();
  await page.goto('http://192.168.3.178:8080/');
  // https://pvp.qq.com/web201605/wallpaper.shtml
  //找节点
  const username = await page.$('#username')
  const password = await page.$('#password')
  // 获取视图宽高
  const documentSize = await page.evaluate(() => {
    return {
      width: document.documentElement.clientWidth,
      height: document.body.clientHeight
    }
  })
  const login = await page.$('#login')
  await setInterval(() => {
    //截屏
    let now = new Date()
    let pngName = now.getSeconds()
    page.screenshot({
      path: `图${pngName}.png`,
      clip: {
        x: 0,
        y: 0,
        width: documentSize.width,
        height: documentSize.height
      }
    });
  }, 50000)

  page.on('response', response => {
    console.log("用户正在操作...")
  });
  // 输入内容
  const ulen = await page.$eval('#username', el => el.value.length);
  const plen = await page.$eval('#password', el => el.value.length);
  await username.focus()
  for (let i = 0; i < ulen; i++) {
    await page.keyboard.press('Backspace');
  }
  await password.focus()
  for (let i = 0; i < plen; i++) {
    await page.keyboard.press('Backspace');
  }
  await username.type('xiaoma', { delay: 200 })
  await password.type('123456', { delay: 200 })
  console.log("输入文字成功")
  //等待
  await page.waitFor(3000)
  // 点击
  await login.click()
  console.log("点击成功")
  // 关闭浏览器
  // await browser.close();
})();